﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using StripeOne.Core;
using StripeOne.Web.StripeOneServiceReference;

namespace StripeOne.Controllers
{
    public class AuthController : StripeOneController
    {
        //
        // GET: /Login?refUrl=AAA

        public ActionResult Index(string refUrl)
        {
            return View();
        }

        [HttpGet] //[HttpPost]
        public ActionResult Login(string username, string password, string refUrl = null)
        {
            //Realiza o login
            User result = new StripeOneServiceClient().Login(username, password);

            if (result.IsLoggedIn && result.Status == Status.OK)
            {
                //Cria a sessão com os dados do usuario
                Session["user"] = result;

                //Cria os cookies
                //var user = CreateCookies(result);

                if (string.IsNullOrEmpty(refUrl))
                    return RedirectToAction("Index", "Home");
                else
                    return Redirect(refUrl);
            }
            else
            {
                ViewBag.ErrorMessage = "Mensagem de erro de login aqui";
                return View();
            }
        }

        [HttpGet] //[HttpPost]
        public ActionResult Logout(string username)
        {
            //Realiza o login
            Status result = new StripeOneServiceClient().Logout(username);

            if (result == Status.OK)
            {
                //Nula a sessão com os dados de usuario
                Session["user"] = null;

                //Apaga os cookies
                //var user = DeleteCookies(result);

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ViewBag.ErrorMessage = "Mensagem de erro de login aqui";
                return View();
            }
        }

        protected override bool RequireAuthentication { get { return false; } }
    }
}
